#!/user/bin/env python
#-*- coding:utf-8 -*-
#time os 是核心库
#r打头的是普通字符 u打头的是uncode编码
import urllib2
import sys
import re



def testArgument():
    if len(sys.argv) != 2:
        print(u'只需要一个参数就够了')
        tipUse()
        exit()
    else:
        TP = TestProxy(sys.argv[1])

def tipUse():
    print(u'该程序只能输入一个参数，这个参数必须是一个可用proxy')
    print(u'usage:python testUrllib2WithProxy.py https://1.2.3.4:5')

    

class TestProxy(object):
    def __init__(self,proxy):
        self.proxy = proxy
        self.checkProxyFormat(self.proxy)
        self.url = 'http://www.baidu.com'
        self.timeout = 5
        self.flagWord = '百度'
        self.useProxy(self.proxy)

    def checkProxyFormat(self,proxy):
        try:
            proxyMatch = re.compile('http[s]?://[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}:[\d]{1,5}$')
            re.search(proxyMatch,proxy).group()
        except AttributeError:
            tipUse()
            exit()
        flag = 1
        proxy = proxy.replace('//','')
        try:
            protocol = proxy.split(':')[0]
            ip = proxy.split(':')[1]
            port = proxy.split(':')[2]
        except IndexError:
            print(u'下标出界')
            tipUse()
            exit()

        flag = flag and len(proxy.split(':')) == 3 and len(ip.split('.'))
        flag = ip.split('.')[0] in map(str,xrange(1,256)) and flag
        flag = ip.split('.')[1] in map(str,xrange(256)) and flag
        flag = ip.split('.')[2] in map(str,xrange(256)) and flag
        flag = ip.split('.')[3] in map(str,xrange(1,255)) and flag

        flag = protocol in [u'http',u'https'] and flag
        flag = port in map(str,range(1,65535)) and flag

        if flag:
            print(u'输入的http代理符合标准')
        else:
            tipUse()
            exit()

    def useProxy(self,proxy):
        protocol = proxy.split('//')[0].replace(':','')
        ip = proxy.split('//')[1]
        opener = urllib2.build_opener(urllib2.ProxyHandler({protocol:ip}))
        urllib2.install_opener(opener)
        try:
            response = urllib2.urlopen(self.url,timeout = self.timeout)
        except:
            print(u'连接错误，退出程序')
            exit()

        str = response.read()

        if re.search(self.flagWord,str):
            print(u'已取得特征词，该代理可用')
        else:
            print(u'该代理不可用')

    def clear():
        print(u'内容较多，显示3秒后翻页')
        time.sleep(2);
        OS = platform.system()
        if(OS == u'Windows'):
            os.system('cls')
        else:
            os.system('clear')

    def linkBaidu():
        url = 'http://www.baidu.com'
        try:
            response = urllib2.urlopen(url,timeout=3)
        except urllib2.URLError:
            print(u"网络地址错误")
            exit()

        with open('./baidu.txt','w') as fp:
            fp.write(response.read())

        print(u"%s" %response.geturl())

if __name__ == '__main__':
    testArgument()
